﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.Lesson")]
    public partial class Lesson : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private short _idLesson;

        private short _refGroup;

        private short _refWeek;

        private string _room;

        private short _numberLesson;

        private EntityRef<Group> _Group;

        private EntityRef<Week> _Week;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnidLessonChanging(short value);
        partial void OnidLessonChanged();
        partial void OnrefGroupChanging(short value);
        partial void OnrefGroupChanged();
        partial void OnrefWeekChanging(short value);
        partial void OnrefWeekChanged();
        partial void OnroomChanging(string value);
        partial void OnroomChanged();
        partial void OnnumberLessonChanging(short value);
        partial void OnnumberLessonChanged();
        #endregion

        public Lesson()
        {
            this._Group = default(EntityRef<Group>);
            this._Week = default(EntityRef<Week>);
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idLesson", AutoSync = AutoSync.OnInsert, DbType = "SmallInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public short idLesson
        {
            get
            {
                return this._idLesson;
            }
            set
            {
                if ((this._idLesson != value))
                {
                    this.OnidLessonChanging(value);
                    this.SendPropertyChanging();
                    this._idLesson = value;
                    this.SendPropertyChanged("idLesson");
                    this.OnidLessonChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refGroup", DbType = "SmallInt NOT NULL")]
        public short refGroup
        {
            get
            {
                return this._refGroup;
            }
            set
            {
                if ((this._refGroup != value))
                {
                    if (this._Group.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefGroupChanging(value);
                    this.SendPropertyChanging();
                    this._refGroup = value;
                    this.SendPropertyChanged("refGroup");
                    this.OnrefGroupChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refWeek", DbType = "SmallInt NOT NULL")]
        public short refWeek
        {
            get
            {
                return this._refWeek;
            }
            set
            {
                if ((this._refWeek != value))
                {
                    if (this._Week.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefWeekChanging(value);
                    this.SendPropertyChanging();
                    this._refWeek = value;
                    this.SendPropertyChanged("refWeek");
                    this.OnrefWeekChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_room", DbType = "VarChar(50) NOT NULL", CanBeNull = false)]
        public string room
        {
            get
            {
                return this._room;
            }
            set
            {
                if ((this._room != value))
                {
                    this.OnroomChanging(value);
                    this.SendPropertyChanging();
                    this._room = value;
                    this.SendPropertyChanged("room");
                    this.OnroomChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_numberLesson", DbType = "SmallInt NOT NULL")]
        public short numberLesson
        {
            get
            {
                return this._numberLesson;
            }
            set
            {
                if ((this._numberLesson != value))
                {
                    this.OnnumberLessonChanging(value);
                    this.SendPropertyChanging();
                    this._numberLesson = value;
                    this.SendPropertyChanged("numberLesson");
                    this.OnnumberLessonChanged();
                }
            }
        }

        [AssociationAttribute(Name = "Group_Lesson", Storage = "_Group", ThisKey = "refGroup", OtherKey = "idGroup", IsForeignKey = true)]
        public Group Group
        {
            get
            {
                return this._Group.Entity;
            }
            set
            {
                Group previousValue = this._Group.Entity;
                if (((previousValue != value)
                     || (this._Group.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Group.Entity = null;
                        previousValue.Lessons.Remove(this);
                    }
                    this._Group.Entity = value;
                    if ((value != null))
                    {
                        value.Lessons.Add(this);
                        this._refGroup = value.idGroup;
                    }
                    else
                    {
                        this._refGroup = default(short);
                    }
                    this.SendPropertyChanged("Group");
                }
            }
        }

        [AssociationAttribute(Name = "Week_Lesson", Storage = "_Week", ThisKey = "refWeek", OtherKey = "idDay", IsForeignKey = true)]
        public Week Week
        {
            get
            {
                return this._Week.Entity;
            }
            set
            {
                Week previousValue = this._Week.Entity;
                if (((previousValue != value)
                     || (this._Week.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Week.Entity = null;
                        previousValue.Lessons.Remove(this);
                    }
                    this._Week.Entity = value;
                    if ((value != null))
                    {
                        value.Lessons.Add(this);
                        this._refWeek = value.idDay;
                    }
                    else
                    {
                        this._refWeek = default(short);
                    }
                    this.SendPropertyChanged("Week");
                }
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}